package langnstats.project;

import java.io.IOException;
import java.util.List;

import langnstats.project.languagemodel.hmm.HMM;
import langnstats.project.languagemodel.interpolation.LinearInterpolation;
import langnstats.project.languagemodel.srilm.NGramLanguageModel;
import langnstats.project.languagemodel.srilm.SriLMParameters.DiscountingMethod;
import langnstats.project.lib.LanguageModel;
import langnstats.project.lib.WordType;
import langnstats.project.lib.crossvalidation.CrossValidationTokenSet;
import langnstats.project.lib.crossvalidation.TrainTokens;

public class Main {
	
	private static LanguageModel[] lmodels;

	private static NGramLanguageModel[] buidlNgram(){
		NGramLanguageModel[] res = new NGramLanguageModel[2];
		int[] discount = new int[3];
		int[] cutoff = new int[3];
		cutoff[0] = 0;
		cutoff[1] = 1;
		cutoff[2] = 2;
		discount[0] = discount[1] = discount[2] = 7;
		res[0] = new NGramLanguageModel(DiscountingMethod.Keyser_Ney,3,cutoff,discount);
		discount = new int[2];
		cutoff = new int[2];
		cutoff[0] = 0;
		cutoff[1] = 1;
		discount[0] = discount[1]  = 7;
		res[1] = new NGramLanguageModel(DiscountingMethod.Good_Turing,2,cutoff,discount);		
		return res;
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Global.debugOut.println("Starting JLMTools...");
		
		Global.debugOut.println("Preparing training-evaluation set...");
		WordType[] trainATokens = WordType.parse(Global.getTrainA());
		WordType[][] tokens = CrossValidationTokenSet.divide(trainATokens, 10);
		
		WordType[] trainTokens = tokens[0];
		WordType[] evalTokens = tokens[1];
				
		Global.debugOut.println("Creating Language Models...");
		
		LinearInterpolation li = new LinearInterpolation();
		//NGramLanguageModel[] models = buidlNgram();
		List<LanguageModel> models = Global.getAllModels();
		
		int modelIndex = models.size();
		
		Global.debugOut.println("Training Language Models...");
		
		for (int i = 0; i<modelIndex; i++) {
			models.get(i).train(new TrainTokens(trainTokens));
			Global.debugOut.println(System.currentTimeMillis() + ": Train Finished -- " + models.get(i).getDescription());
			li.addLanguageModel(models.get(i));
		}
		Global.debugOut.println("Testing Langugage Models...");
		
		double min_ll = li.test(new TrainTokens(evalTokens));
		li.setStopLL(min_ll);

		li.train(new TrainTokens(evalTokens));
		
		Global.debugOut.println("Program ended normally!");
	}

}
